package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.files.RestUploadFileInfo;
import com.sxbbc.common.basics.files.UploadUtils;
import com.sxbbc.common.core.entity.jz.BaseInfo;
import com.sxbbc.common.core.mapper.jz.BaseInfoMapper;
import com.sxbbc.common.core.service.IBaseInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 平台基础信息表 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-04-28
 */
@Service
public class BaseInfoServiceImpl extends ServiceImpl<BaseInfoMapper, BaseInfo> implements IBaseInfoService {
    @Override
    public RestResponse editSysBaseInfo(MultipartFile pcLogo, MultipartFile appLogo, MultipartFile andUrl,
                                        MultipartFile iosUrl, MultipartFile wxUrl, BaseInfo info) {
        if (StringUtils.isBlank(info.getWebsiteName())) {
            return GetRest.getFail("请输入网站名称");
        }
        if (StringUtils.isBlank(info.getWebsiteKey())) {
            return GetRest.getFail("请设置网站关键词");
        }
        if (StringUtils.isBlank(info.getWebsiteDesc())) {
            return GetRest.getFail("请输入网站描述");
        }
        if (StringUtils.isBlank(info.getCopyright())) {
            return GetRest.getFail("请输入版权信息");
        }
        if (StringUtils.isBlank(info.getIcpVal())) {
            return GetRest.getFail("请输入备案号信息");
        }
        UploadUtils upload = UploadUtils.getInstance();
        String imgUrl = "";
        // 文件上传
        if (pcLogo != null) {
            RestUploadFileInfo pc = upload.uploadFileQN(pcLogo);
            if (!pc.isStatus()) {
                return GetRest.getFail("PC端logo上传失败");
            }
            imgUrl = pc.getData();
            info.setPcLogo(imgUrl);
        }
        if (appLogo != null) {
            RestUploadFileInfo app = upload.uploadFileQN(appLogo);
            if (!app.isStatus()) {
                return GetRest.getFail("App端logo上传失败");
            }
            imgUrl = app.getData();
            info.setAppLogo(imgUrl);
        }
        if (andUrl != null) {
            RestUploadFileInfo android = upload.uploadFileQN(andUrl);
            if (!android.isStatus()) {
                return GetRest.getFail("安卓二维码上传失败");
            }
            imgUrl = android.getData();
            info.setAndroidQr(imgUrl);
        }
        if (iosUrl != null) {
            RestUploadFileInfo ios = upload.uploadFileQN(iosUrl);
            if (!ios.isStatus()) {
                return GetRest.getFail("iOS二维码上传失败");
            }
            imgUrl = ios.getData();
            info.setIosQr(imgUrl);
        }
        if (wxUrl != null) {
            RestUploadFileInfo wx = upload.uploadFileQN(wxUrl);
            if (!wx.isStatus()) {
                return GetRest.getFail("微信二维码上传失败");
            }
            imgUrl = wx.getData();
            info.setWxQr(imgUrl);
        }
        boolean save = super.saveOrUpdate(info);
        if (!save) {
            throw new ThrowJsonException("基础信息保存失败");
        }
        return GetRest.getSuccess("保存成功");
    }
}
